package cc.liyinghao.service.impl;

import cc.liyinghao.common.en.ContactStatus;
import cc.liyinghao.common.pojo.SysAddContacts;
import cc.liyinghao.common.pojo.SysContacts;
import cc.liyinghao.response.AddUserListVo;
import cc.liyinghao.response.ContactsListVo;
import cc.liyinghao.config.DefaultIpPrefixConfig;
import cc.liyinghao.mapper.SysAddContactsMapper;
import cc.liyinghao.mapper.SysContactsMapper;
import cc.liyinghao.security.util.SecurityUtil;
import cc.liyinghao.service.ContactsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 好友关系服务实现类
 */
@Service
@RequiredArgsConstructor
public class ContactsServiceImpl implements ContactsService {

    private final DefaultIpPrefixConfig defaultIpPrefixConfig;
    private final SysAddContactsMapper sysAddContactsMapper;
    private final SysContactsMapper sysContactsMapper;

    /**
     * 添加好友请求
     */
    @Override
    public int addFriendship(int id) {
        //1.先查看原本是否有未同意的请求
        LambdaQueryWrapper<SysAddContacts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(SysAddContacts::getId)
                .eq(SysAddContacts::getUserId, SecurityUtil.getCurrentUserId())
                .eq(SysAddContacts::getContactUserId, id);
        SysAddContacts addContacts = sysAddContactsMapper.selectOne(queryWrapper);
        if (!Objects.isNull(addContacts)) {
            throw new RuntimeException("已发送过请求");
        }
        //2.添加好友请求
        SysAddContacts sysAddContacts = new SysAddContacts();
        sysAddContacts.setUserId(SecurityUtil.getCurrentUserId());
        sysAddContacts.setContactUserId(id);
        sysAddContactsMapper.insert(sysAddContacts);
        return sysAddContacts.getId();
    }

    @Override
    public List<AddUserListVo> getNewAddList() {
        List<AddUserListVo> addUserListVoList = sysAddContactsMapper.getNewAddList(SecurityUtil.getCurrentUserId());
        addUserListVoList.forEach(item -> {
            item.setHeadImgUrl(defaultIpPrefixConfig.getDefaultStaticIp() + item.getHeadImgUrl());
        });
        return addUserListVoList;
    }

    @Override
    public List<AddUserListVo> getNewAddListOther() {
        List<AddUserListVo> addUserListVoList = sysAddContactsMapper.getNewAddListOther(SecurityUtil.getCurrentUserId());
        addUserListVoList.forEach(item -> {
            item.setHeadImgUrl(defaultIpPrefixConfig.getDefaultStaticIp() + item.getHeadImgUrl());
        });
        return addUserListVoList;
    }

    @Override
    @Transactional
    public int sureAddFriendship(int id) {
        //1.修改添加好友表的状态 sys_add_contacts
        LambdaQueryWrapper<SysAddContacts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper //这里有一个容易错的地方 用户本人的id为要被添加的id
                .eq(SysAddContacts::getUserId, id)
                .eq(SysAddContacts::getContactUserId, SecurityUtil.getCurrentUserId());
        SysAddContacts addContacts = new SysAddContacts();
        addContacts.setStatus(ContactStatus.ADDED.toByte()); //设置为同意
        sysAddContactsMapper.update(addContacts, queryWrapper);
        //2.添加好友关系 sys_contacts
        SysContacts sysContacts = new SysContacts();
        sysContacts.setUserId(SecurityUtil.getCurrentUserId());
        sysContacts.setContactUserId(id);
        return sysContactsMapper.insert(sysContacts);
    }

    @Override
    public List<ContactsListVo> getContactsList() {
        List<ContactsListVo> contactsList = sysContactsMapper.getContactsListByUserId(SecurityUtil.getCurrentUserId());
        contactsList.forEach(contact ->
                contact.setHeadImgUrl(defaultIpPrefixConfig.getDefaultStaticIp() + contact.getHeadImgUrl())
        );
        return contactsList;
    }

    @Override
    public boolean isContact(int id, int contactId) {
        return sysContactsMapper.isContact(id, contactId) > 0;
    }
}
